<?php
# Check if User is registered for AJAX requests
if(isset($_GET['f_login']))
{
	$respond = TRUE;
	if($FILTER->isValidAlpha($_GET['f_login']))
	{
		$DB->DB_QueryBuilder('SELECT `login` FROM `meccano_users` WHERE `login` = "%s"', array($_GET['f_login']));
		$DB->DB_Fetch();
		if($DB->rows == 1)
		{
			$respond = FALSE;
		}
	}

	echo json_encode($respond);
	exit();
}


# Redirect user to the personal account page if he is registered
if(session::user())
{
   header('location: '. $categories[20]['href']);
   exit();
}


# Register user
if(isset($_POST))
{
   $data = array();
   foreach($_POST AS $k=>$v)
   {
      if(strpos($k, 'f_') === 0)
      {
         $data[substr($k, 2)] = $v;
      }
   }

   if(!empty($data))
   {
      $data['key'] = md5(rand_string(3).time());
      $USERS->attrSetter('required', array('login','email','surname','name','phone','country','zip','city','region','address'));
      $USERS->addUser($data);
   }
}